// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import fastClick from 'fastclick'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import Vuex from 'vuex'
import wx from 'weixin-js-sdk'
import 'babel-polyfill'
import 'styles/reset.css'
import 'styles/iconfont.css'
import 'styles/border.css'
import 'swiper/dist/css/swiper.css'
import 'vue2-animate/dist/vue2-animate.min.css'
import Mint from 'mint-ui'

Vue.config.productionTip = false
fastClick.attach(document.body)
Vue.use(VueAwesomeSwiper)
Vue.use(Vuex)
Vue.use(Mint)
Vue.use(wx)

const ADD_COUNT = 'ADD_COUNT'
const REMOVE_COUNT = 'REMOVE_COUNT'
var store = new Vuex.Store({
  state: {
    token: {},
    userId: ''
  },
  mutations: {
    [ADD_COUNT] (state, token) {
      localStorage.setItem('userId', token.id)
      state.userId = token.id
      localStorage.setItem('token', JSON.stringify(token))
      state.token = token
      let timer = Date.now() + 1000 * 60 * 60 * 3
      localStorage.setItem('timer', timer)
      state.timer = timer
    },
    [REMOVE_COUNT] (state, token) {
      state.userId = null
      state.token = null
      state.timer = null
      localStorage.removeItem('userId')
      localStorage.removeItem('token')
      localStorage.removeItem('timer')
    }
  }
})
// router.beforeEach((to, from, next) => {
//   if (!store.state.user.id && to.path != '/author') {
//     // 第一次进入项目
//     holdno.cookie.set('beforeLoginUrl', to.fullPath) // 保存用户进入的url
//     next('/author')
//     return false
//   }
//   next()
// })
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  store.state.token = localStorage.getItem('token')
  store.state.userId = localStorage.getItem('userId')
  store.state.timer = localStorage.getItem('timer')
  let nowTime = Date.now()
  if (localStorage.getItem('timer') < nowTime && localStorage.getItem('timer') !== null) {
    // alert(111)
    store.commit('REMOVE_COUNT', store.state.token)
  }
  // console.log(to.name)
  // if (!store.state.userId && to.path !== '/author' && to.path !== '/login' && to.path !== '/register') {
  if (!store.state.userId && to.path !== '/author' && to.path !== '/login' && to.path !== '/register' && to.path !== '/privacy') {
    // 第一次进入项目
    // 0
    sessionStorage.setItem('beforeLoginUrl', to.fullPath)
    sessionStorage.setItem('shareId', to.query.shareId)
    // console.log('分享', sessionStorage.getItem('shareId'))
    if (to.query.code) {
      sessionStorage.setItem('wechatCode', to.query.code)
    }
    // alert(to.query.code)
    if (to.query.state) {
      sessionStorage.setItem('wechatState', to.query.state)
    }
    if (to.name === 'Detail') {
      sessionStorage.setItem('DetailPath', to.path)
    }
    // holdno.cookie.set('beforeLoginUrl', to.fullPath) // 保存用户进入的url
    next('/author')
    return false
  }
  // next()
  // XXX: 修复iOS版微信HTML5 History兼容性问题
  if (to.path !== location.pathname) {
    // 此处不可使用location.replace
    location.assign(to.fullPath)
  } else {
    next()
  }
  // if (to.meta.requireAuth) {
  //   if (store.state.userId) {
  //     next()
  //   } else {
  //     next({
  //       path: '/login',
  //       query: { redirect: to.fullPath }
  //     })
  //   }
  // } else {
  //   next()
  // }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
